﻿using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Application.Components.Platform
{
    public interface IPosCashierService : IApplicationService
    {
        Task<PosCashier[]> AttachWarehouse(PosCashier[] data);
        Task<PosCashRegister[]> AttachWarehouseForCashierRegister(PosCashRegister[] data);
    }

    public class PosCashierService : MyApplicationService, IPosCashierService
    {
        private readonly IRepository<PosCashier> posCashiers;
        public PosCashierService(IRepository<PosCashier> posCashiers)
        {
            this.posCashiers = posCashiers;
        }

        public async Task<PosCashier[]> AttachWarehouse(PosCashier[] data)
        {
            var db = await this.posCashiers.GetDbContextAsync();

            await db.AttachData<Warehouse.Entity.Warehouse, PosCashier>(data,
                idGetter: x => x.warehouseID,
                action: (m, arr) => m.Warehouse = arr.FirstOrDefault());

            return data;
        }

        public async Task<PosCashRegister[]> AttachWarehouseForCashierRegister(PosCashRegister[] data)
        {
            var db = await this.posCashiers.GetDbContextAsync();

            await db.AttachData<Warehouse.Entity.Warehouse, PosCashRegister>(data,
                idGetter: x => x.warehouseID,
                action: (m, arr) => m.Warehouse = arr.FirstOrDefault());

            return data;
        }
    }
}
